package com.hongning.railway.ui.webview;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.widget.LinearLayout;

import com.hongning.railway.R;
import com.hongning.railway.app.AppViewModelFactory;
import com.hongning.railway.app.Constant;
import com.hongning.railway.databinding.FragmentAppWebviewBinding;
import com.hongning.railway.entity.reqbean.ReqAddPages;
import com.hongning.railway.util.Apputils;
import com.just.agentweb.AgentWeb;
import com.just.agentweb.WebChromeClient;
import com.just.agentweb.WebViewClient;

import io.reactivex.functions.Consumer;
import me.goldze.mvvmhabit.BR;
import me.goldze.mvvmhabit.base.BaseFragment;
import me.goldze.mvvmhabit.base.BaseViewModel;
import me.goldze.mvvmhabit.bus.Messenger;
import me.goldze.mvvmhabit.utils.KLog;
import me.goldze.mvvmhabit.utils.RxUtils;

/**
 * Author: zhangjing
 * DATA:2019-08-26 16:45
 * DESC:
 * email:zhangjingworkmol@163.com
 */
public class AppWebViewFragment extends BaseFragment<FragmentAppWebviewBinding, BaseViewModel> {

    private String title;
    private String url;
    private String id;
    @Override
    public int initContentView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return R.layout.fragment_app_webview;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }

    @Override
    public void initData() {
        super.initData();
        binding.appTitle.ivLeft.setVisibility(View.VISIBLE);
        binding.appTitle.ivLeft.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getActivity().finish();
            }
        });
        if (!TextUtils.isEmpty(title)){
            binding.appTitle.tvTopTitle.setText(title);
        }

        initAgentWeb();
        if (!TextUtils.isEmpty(id)){
            ReqAddPages reqAddPages = new ReqAddPages();
            reqAddPages.setNewsId(id);
            reqAddPages.setUserId(Apputils.getUserId());
            AppViewModelFactory.getInstance(getActivity().getApplication()).getmRepository().addPageView(reqAddPages)
                    .compose(RxUtils.schedulersTransformer())
                    .compose(this.bindToLifecycle())
                    .subscribe(new Consumer() {
                        @Override
                        public void accept(Object o) throws Exception {
                            KLog.d("记录添加成功");
                            Messenger.getDefault().sendNoMsg(Constant.LESSON_REFRESH);
                        }
                    }, new Consumer<Throwable>() {
                        @Override
                        public void accept(Throwable throwable) throws Exception {

                        }
                    });
        }
    }

    @Override
    public void initParam() {
        super.initParam();
        title = getArguments().getString(Constant.EXTRA_TITLE);
        url = getArguments().getString(Constant.EXTRA_STRING);
        id = getArguments().getString(Constant.EXTRA_ID);
    }

    private void initAgentWeb(){
        WebViewClient mWebViewClient=new WebViewClient(){
            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                //do you  work
            }
        };
        WebChromeClient mWebChromeClient=new WebChromeClient(){
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                //do you work
            }
        };
        AgentWeb.with(this)
                .setAgentWebParent(binding.llContent,new LinearLayout.LayoutParams(-1,-1) )
                .useDefaultIndicator()
                .setWebChromeClient(mWebChromeClient)
                .setWebViewClient(mWebViewClient)
                .setSecurityType(AgentWeb.SecurityType.STRICT_CHECK)
                .createAgentWeb()
                .ready()
                .go(getUrl());

    }

    private String getUrl() {
        return url;
    }


}
